package com.proton.carepatchtemp.activity;

import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.adapter.DeviceManagerAdapter;
import com.proton.carepatchtemp.base.BaseActivity;
import com.proton.carepatchtemp.constant.Codes;
import com.proton.carepatchtemp.constant.Settings;
import com.proton.carepatchtemp.database.daomanager.DeviceManager;
import com.proton.carepatchtemp.database.table.Device;
import com.proton.carepatchtemp.utils.NetUtils;
import com.proton.carepatchtemp.utils.TastyUtils;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;
import com.proton.carepatchtemp.utils.net.DeviceCenter;
import com.sdsmdg.tastytoast.TastyToast;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.refreshlayout.BGANormalRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

public class DeviceManagerActivity extends BaseActivity implements BGARefreshLayout.BGARefreshLayoutDelegate {

    private ImageButton mBtnBack;
    private RecyclerView mRcvDeviceList;
    private BGANormalRefreshViewHolder mBGANormalRefreshViewHolder;
    private List<Device> mDevices;
    private LinearLayoutManager mLinearLayoutManager;
    private DeviceManagerAdapter mManagerAdapter;
    private BGARefreshLayout mBgaRflDevice;
    /**
     * 您还没有连接过任何设备
     */
    private TextView mTvDvcManagerBitmap;

    @Override
    public void dealEvent(MainEvent event) {
        switch (event.getId()) {
            case Codes.DEVICE_GET_LIST:
                if (event.isSuccess()) {
                    mDevices.clear();
                    mDevices.addAll(DeviceManager.getAll());
                    mManagerAdapter.notifyDataSetChanged();

                } else if (!event.isSuccess()) {
                    TastyUtils.makeTextShort(R.string.get_device_failed, TastyToast.ERROR);
                }
                if (mDevices.size() <= 0) {
                    mTvDvcManagerBitmap.setVisibility(View.VISIBLE);
                } else {
                    mTvDvcManagerBitmap.setVisibility(View.INVISIBLE);
                }
                mBgaRflDevice.endRefreshing();
//                TastyUtils.makeText(mDevices.size(),TastyToast.LENGTH_LONG,TastyToast.SUCCESS);
                break;
            case Codes.DEVICE_DELETE:
                if (event.isSuccess()) {
                    TastyUtils.makeTextShort(R.string.delete_device_success, TastyToast.SUCCESS);
                } else {
                    if (Settings.isTest) {
                        TastyUtils.makeTextShort(event.getMsg(), TastyToast.ERROR);
                    } else {
                        TastyUtils.makeTextShort(R.string.delete_device_failed, TastyToast.ERROR);
                    }
                    mDevices.clear();
                    mDevices.addAll(DeviceManager.getAll());
                    mManagerAdapter.notifyDataSetChanged();
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_manager);
        initView();
        initData();
        setBgaRefreshLayout();
        setListener();
    }

    private void initData() {
        mDevices = new ArrayList<>();
        mDevices.addAll(DeviceManager.getAll());
        if (mDevices.size() <= 0) {
            mTvDvcManagerBitmap.setVisibility(View.VISIBLE);
        } else {
            mTvDvcManagerBitmap.setVisibility(View.INVISIBLE);
        }
        mLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mManagerAdapter = new DeviceManagerAdapter(this, mDevices);
        mRcvDeviceList.setLayoutManager(mLinearLayoutManager);
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        mRcvDeviceList.addItemDecoration(dividerItemDecoration);
        mRcvDeviceList.setAdapter(mManagerAdapter);
    }

    private void setBgaRefreshLayout() {
        mBGANormalRefreshViewHolder = new BGANormalRefreshViewHolder(this, true);
        mBGANormalRefreshViewHolder.setLoadingMoreText(getString(R.string.loading));
        mBGANormalRefreshViewHolder.setPullDownRefreshText(getString(R.string.string_pull_2_refresh));
        mBGANormalRefreshViewHolder.setRefreshingText(getString(R.string.loading));
        mBGANormalRefreshViewHolder.setReleaseRefreshText(getString(R.string.string_release_text));
        mBgaRflDevice.setDelegate(this);
        mBgaRflDevice.setRefreshViewHolder(mBGANormalRefreshViewHolder);
    }

    @Override
    protected void setListener() {
        mBtnBack.setOnClickListener(view -> {
            finish();
        });
    }

    @Override
    public void onEventMainThread(MainEvent event) {
        dealOnEvent(event);
    }

    private void initView() {
        mBtnBack = findViewById(R.id.btn_back);
        mRcvDeviceList = findViewById(R.id.rcv_device_list);
        mBgaRflDevice = findViewById(R.id.bga_rfl_device);
        mTvDvcManagerBitmap = findViewById(R.id.tv_dvc_manager_bitmap);
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        if (NetUtils.noNet()) {
            mBgaRflDevice.endRefreshing();
            TastyUtils.makeTextShort(R.string.have_no_netconnect, TastyToast.CONFUSING);
            return;
        }
        DeviceCenter.getList();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        return false;
    }
}
